package com.zdzx.controller.admin.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdzx.common.result.PageInfo;
import com.zdzx.common.result.PageUtil;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.RoleFunctionMapper;
import com.zdzx.mapper.RoleMapper;
import com.zdzx.mapper.UserRoleMapper;
import com.zdzx.model.service.base.vo.Role;
import com.zdzx.model.service.base.vo.RoleFunction;
import com.zdzx.model.service.base.vo.UserRole;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "角色")
@RestController
@RequestMapping("/admin")
public class RoleController {


	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RoleFunctionMapper roleFunctionMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@ApiOperation(value = "获取角色", notes = "")
	@GetMapping("/role")
	public Result<PageInfo<Role>> getList(@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@ApiParam("") @RequestParam(value = "roleName", required = false) String roleName){
		if (pageNum == null || pageSize == null) {
			pageNum = 1;
			pageSize = 10;
		}
		QueryWrapper<Role> queryWrapper=new QueryWrapper<Role>();
		if(!StringUtils.isEmpty(roleName)) {
		queryWrapper.eq("name", roleName);
		}
		Page<Role> page=new Page<Role>(pageNum,pageSize);;
		 IPage<Role> info = roleMapper.selectPage(page, queryWrapper);
		PageInfo<Role> data = PageUtil.data(info.getPages(), info.getSize(), info.getTotal(), info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}
	
	@ApiOperation(value = "添加", notes = "")
	@PostMapping("/role")
	public Result<String> save(@RequestBody Role entity){
		int rows = roleMapper.insert(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}

	@ApiOperation(value = "修改", notes = "")
	@PutMapping("/role")
	public Result<String> edit(@RequestBody Role entity){
		int rows = roleMapper.insert(entity);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}

	@ApiOperation(value = "删除", notes = "")
	@DeleteMapping("/role/{roleId}")
	public Result<String> del(@PathVariable("roleId") String roleId){
		int rows = roleMapper.deleteById(roleId);
		return ResultUtil.data(rows>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	@ApiOperation(value = "设置用户角色", notes = "")
	@PostMapping("/role/setRoleResouce/{groupId}")
	public Result<String> setUserRole(
			@ApiParam("") @PathVariable("systemId")String systemId,
			@ApiParam("") @RequestBody List<String> groupIds){
		QueryWrapper<UserRole> wrapper=new QueryWrapper<UserRole>();
		wrapper.eq("user_id",systemId);
		userRoleMapper.delete(wrapper);
		for (String string : groupIds) {
			UserRole entity=new UserRole();
			entity.setRoleId(string);
			entity.setUserId(systemId);
			userRoleMapper.insert(entity);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "设置角色功能", notes = "")
	@PostMapping("/role/fun")
	public Result<String> saveRoleFun(@RequestBody List<RoleFunction> roleFunctions){
		if(roleFunctions!=null&&roleFunctions.size()>0) {
		QueryWrapper<RoleFunction> wrapper=new QueryWrapper<RoleFunction>();
		wrapper.eq("role_id",roleFunctions.get(0).getRoleId());
		roleFunctionMapper.delete(wrapper);
		}
		for (RoleFunction roleFunction : roleFunctions) {
			roleFunctionMapper.insert(roleFunction);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}

}
